package com.vanchu.colorful;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.widget.Toast;

public class ToastPlugin extends CordovaPlugin {

	@Override 
	public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException { 
		if (action.equals("Toast")) {
			int tmpDur;
			String tmpText = "none";
			try {
				tmpText = data.getString(0);
			} catch (JSONException e) {
				callbackContext.error("Parsing Text Exception");
				return false;
			} 
			try {
				tmpDur = data.getInt(1);
			} catch (JSONException e) {
				callbackContext.error("Parsing Duration Exception");
				return false;
			}
			
			final String Text = tmpText;
			final int Duration = tmpDur;
			final CordovaInterface currentCtx = cordova;
			
			Runnable runnable = new Runnable() { 
				public void run() {
					Toast.makeText(currentCtx.getActivity(), Text, Duration).show();
				 }
			}; 
			currentCtx.getActivity().runOnUiThread(runnable);
			
			callbackContext.success("ok");
			return true;
        }
		return false;
	}

}
